/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.server;

import ftpcombiner.ApplicationController;
import ftpcombiner.server.serverconnection.File;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import javax.swing.JProgressBar;

/**
 *
 * @author Ben
 */
public class ProgressBarUpdater implements FTPDataTransferListener {

    private JProgressBar progressBar;
    private static ProgressBarUpdater progressBarUpdater;
    private static long size;
    private double progress = 0;
    private ProgressBarThread progressBarThread;

    public static ProgressBarUpdater getInstance(File file) {
        return getInstance(file.getAlias().getSize());
    }

    public static ProgressBarUpdater getInstance(java.io.File file) {
        return getInstance(file.length());
    }
    public static ProgressBarUpdater getInstance(long size){
        if (progressBarUpdater == null) {
            progressBarUpdater = new ProgressBarUpdater();
        }
        ProgressBarUpdater.size = size;
        return progressBarUpdater;
    }

    private ProgressBarUpdater() {
        try {
            progressBar = ApplicationController.getInstance().getApplicationFrame().getStatusBar().getProgressBar();
        } catch (NullPointerException e) {
        }
        if (progressBar != null) {
            progressBarThread = new ProgressBarThread();
        }
    }

    @Override
    public void started() {
        progress = 0;
        progressBar.setVisible(true);
        try {
            progressBarThread.start();
        } catch (IllegalThreadStateException ex) {
            synchronized (progressBarThread) {
                progressBarThread.notify();
            }
        }
    }

    @Override
    public void transferred(int length) {
        progress += (length * 100.0) / (size * 1.0);
    }

    @Override
    public void completed() {
        progressBar.setVisible(false);
        progressBarThread.pause();
    }

    @Override
    public void aborted() {
        progressBar.setVisible(false);
        progressBarThread.pause();
    }

    @Override
    public void failed() {
        progressBar.setVisible(false);
        progressBarThread.pause();
    }

    private class ProgressBarThread extends Thread {

        protected final Object lock = new Object();
        private boolean pause = false;

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                }
                synchronized (lock) {
                    progressBar.setValue((int) progress);
                }
                if (pause) {
                    synchronized (this) {
                        try {
                            wait();
                            pause = false;
                        } catch (InterruptedException ex) {
                        }
                    }
                }
            }
        }

        protected synchronized void pause() {
        }
    }
}
